from myapp import db
from datetime import datetime


class ResourceType(db.Model):
    __tablename__ = 'sys_resource_type'
    id = db.Column(db.String(36), primary_key=True)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)
    update_time = db.Column(db.DateTime, index=True, default=datetime.now)
    name = db.Column(db.String(100))
    description = db.Column(db.String(200))

    resources = db.relationship('Resource', backref='type', lazy='dynamic')

    def to_json(self):
        return {
            'id': self.id,
            'create_time': self.create_time,
            'update_time': self.update_time,
            'name': self.name,
            'description': self.description
        }

    def __repr__(self):
        return '<ResourceType %r>\n' %(self.name)